package yannan.voiplock4ble.utils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.CardView;
import android.support.v7.widget.ListPopupWindow;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.List;

import yannan.voiplock4ble.R;
import yannan.voiplock4ble.base.MyApplication;
import yannan.voiplock4ble.bean.CommonResponse;
import yannan.voiplock4ble.bean.LockBean;
import yannan.voiplock4ble.bean.LockKeyBean;
import yannan.voiplock4ble.bean.VeinKeyBean;
import yannan.voiplock4ble.db.DaoUtils;
import yannan.voiplock4ble.db.MyDBHelper;
import yannan.voiplock4ble.db.daomanager.LockKeyDaoManager;
import yannan.voiplock4ble.db.daomanager.VeinKeyDaoManager;
import yannan.voiplock4ble.net.NetUtils;
import yannan.voiplock4ble.net.base.NetworkCallBack;
import yannan.voiplock4ble.view.MyProgressDialog;

/**
 * <p>name:</p>
 * <p>describe: 提示框工具类</p>
 *
 * @author Maintel
 * @time 2016/9/21 17:44
 */
public class PopShowUtils {

    private static ProgressDialog dialog;

    /**
     * 等待提示框
     *
     * @param mContext
     */
    public static void showProgress(Context mContext) {
        MyProgressDialog.showPro(mContext, false);
    }

    /**
     * 关闭等待提示框
     */
    public static void dimssProgress() {
        MyProgressDialog.dismissDialog();
    }

    /**
     * 修改锁名称
     *
     * @param context
     * @param e
     */
    public static void editLockName(final Context context, final LockBean lockBean, final IOnEditName e) {
        final AlertDialog adb = new AlertDialog.Builder(context).create();
        CardView cardView = (CardView) LayoutInflater.from(context).inflate(R.layout.dialog_edit_name, null);
        adb.setView(cardView);
        adb.show();
        Window window = adb.getWindow();
        window.setContentView(R.layout.dialog_edit_name);
        adb.setCancelable(false);
        adb.setCanceledOnTouchOutside(false);
        final EditText editText = (EditText) window.findViewById(R.id.et_name);
        editText.setHint(lockBean.getLockName());
        TextView left = (TextView) window.findViewById(R.id.tv_left);
        TextView right = (TextView) window.findViewById(R.id.tv_right);
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adb.dismiss();
            }
        });
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String msg = editText.getText().toString().trim();
                if (msg.equals("")) {
                    ToastUtils.showToast(context, "请输入锁名称");
                    return;
                }
                NetUtils.getNetworkService().updateLockName(lockBean.lockserialId,
                        MyApplication.getInterface().userBean.userId, msg).enqueue(new NetworkCallBack<CommonResponse>() {
                    @Override
                    public void onSuc(CommonResponse resBody) {
                        if (resBody.returnValue.equals("1")) {
                            e.onChange(msg);
                        } else {
                            ToastUtils.showToast(context, "修改失败，请重试");
                        }
                    }

                    @Override
                    public void onFail(String msg) {
                        ToastUtils.showToast(context, "修改失败，请重试");
                    }
                });
                adb.dismiss();
            }
        });
        adb.getWindow().setBackgroundDrawable(new ColorDrawable());
        WindowManager.LayoutParams lp = adb.getWindow().getAttributes();
        lp.width = DeviceUtils.getPhoneWidth(context) - 60;//定义宽度
        adb.getWindow().setAttributes(lp);
        adb.show();
    }

    public interface IAddVeinDiaClickListener {
        void onClick(String msg, int power);
    }

    /**
     * 修改备注名字
     *
     * @param context
     * @param e
     */
    public static void editRemarks(final Context context, final LockKeyBean keyBean, final IOnEditName e) {
        final AlertDialog adb = new AlertDialog.Builder(context).create();
        CardView cardView = (CardView) LayoutInflater.from(context).inflate(R.layout.dialog_edit_name, null);
        adb.setView(cardView);
        adb.show();
        Window window = adb.getWindow();
        window.setContentView(R.layout.dialog_edit_name);
        adb.setCancelable(false);
        adb.setCanceledOnTouchOutside(false);
        final EditText editText = (EditText) window.findViewById(R.id.et_name);
        TextView tv_msg = (TextView) window.findViewById(R.id.tv_msg);
        tv_msg.setText("请输入备注名");
        editText.setHint(keyBean.getNickName() == null ? "" : keyBean.getNickName());
        TextView left = (TextView) window.findViewById(R.id.tv_left);
        TextView right = (TextView) window.findViewById(R.id.tv_right);
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adb.dismiss();
            }
        });
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String msg = editText.getText().toString().trim();
                if (msg.equals("")) {
                    ToastUtils.showToast(context, "请输入名字");
                    return;
                }
                NetUtils.getNetworkService().editLockUserRemark(keyBean.lockserialId, keyBean.type, keyBean.userIdentifier,
                        MyApplication.getInterface().userBean.userId, msg).enqueue(new NetworkCallBack<CommonResponse>() {
                    @Override
                    public void onSuc(CommonResponse resBody) {
                        if (resBody.returnValue.equals("1")) {
                            e.onChange(msg);
                            MyDBHelper.upDateLocalTime(keyBean.lockserialId, TimeUtils.getNowTimeyMdHMS(), keyBean.type);
                        } else {
                            ToastUtils.showToast(context, "修改失败，请重试");
                        }
                    }

                    @Override
                    public void onFail(String msg) {
                        ToastUtils.showToast(context, "修改失败，请重试");
                    }
                });
                adb.dismiss();
            }
        });
        adb.getWindow().setBackgroundDrawable(new ColorDrawable());
        WindowManager.LayoutParams lp = adb.getWindow().getAttributes();
        lp.width = DeviceUtils.getPhoneWidth(context) - 60;//定义宽度
        adb.getWindow().setAttributes(lp);
        adb.show();
    }

    /**
     * 添加指静脉
     *
     * @param context
     * @param e
     */
    public static void addVeinKey(final Context context, final IAddVeinDiaClickListener e) {
        final AlertDialog adb = new AlertDialog.Builder(context).create();
        CardView cardView = (CardView) LayoutInflater.from(context).inflate(R.layout.dialog_add_vein, null);
        adb.setView(cardView);
//        adb.show();
//        Window window = adb.getWindow();
//        window.setContentView(R.layout.dialog_add_vein);
        final EditText editText = (EditText) cardView.findViewById(R.id.et_change_name);
        Button btn_ok = (Button) cardView.findViewById(R.id.tv_right);
        Button btn_cancel = (Button) cardView.findViewById(R.id.tv_left);
        RadioGroup rdg_vein_power = (RadioGroup) cardView.findViewById(R.id.rdg_vein_power);
        final int[] power = {-1};
        rdg_vein_power.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rdb_vein_admin) {
                    power[0] = 0;
                } else if (checkedId == R.id.rdb_vein_common) {
                    power[0] = 1;
                }
            }
        });
        adb.setCancelable(false);
        adb.setCanceledOnTouchOutside(false);
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg = editText.getText().toString().trim();
                if (msg.equals("") || msg == null) {
                    ToastUtils.showToast(context, "输入不合法");
                    return;
                }
                if (!CommonUtils.validPhone(msg)) {
                    ToastUtils.showToast(context, "请输入正确的手机号");
                    return;
                }
                if (power[0] == -1) {
                    ToastUtils.showToast(context, "请选择权限");
                    return;
                }

                List<LockKeyBean> tempList = DaoUtils.getDao(LockKeyDaoManager.class).loadAll();
                if (!tempList.isEmpty()) {
                    for (LockKeyBean lockKeyBean : tempList) {
                        if (lockKeyBean.getUserIdentifier().equals(editText.getText().toString().trim())) {
                            ToastUtils.showToast(context, "对不起,该手机号已存在！请重新输入");
                            editText.setText("");
                            editText.requestFocus();
                            return;
                        }
                    }
                }
                e.onClick(msg, power[0]);
                adb.dismiss();
            }
        });

        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                e.onClick(null, 0);
            }
        });

        adb.getWindow().setBackgroundDrawable(new ColorDrawable());
        WindowManager.LayoutParams lp = adb.getWindow().getAttributes();
        lp.width = DeviceUtils.getPhoneWidth(context) - 100;//定义宽度
        adb.getWindow().setAttributes(lp);
        adb.show();
    }

    public static AlertDialog createHintDialog(Context context, boolean isCancel, String msg) {
        final AlertDialog adb = new AlertDialog.Builder(context).create();
        CardView cardView = (CardView) LayoutInflater.from(context).inflate(R.layout.dialog_common, null);
        adb.setView(cardView);
        adb.show();
        Window window = adb.getWindow();
        window.setContentView(R.layout.dialog_common);
        TextView left = (TextView) window.findViewById(R.id.tv_left);
        TextView tv_line = (TextView) window.findViewById(R.id.tv_line);
        TextView right = (TextView) window.findViewById(R.id.tv_right);
        TextView tv_msg = (TextView) window.findViewById(R.id.tv_msg);
        adb.setCancelable(isCancel);
        adb.setCanceledOnTouchOutside(isCancel);
        tv_msg.setText(msg);
        left.setVisibility(View.GONE);
        tv_line.setVisibility(View.GONE);
        right.setText("确定");
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adb.dismiss();
            }
        });
        adb.getWindow().setBackgroundDrawable(new ColorDrawable());
        WindowManager.LayoutParams lp = adb.getWindow().getAttributes();
        lp.width = DeviceUtils.getPhoneWidth(context) - 60;//定义宽度
        adb.getWindow().setAttributes(lp);
        adb.show();
        return adb;
    }

    public static AlertDialog createHintDialog1(Context context, boolean isCancel, String msg) {
        final AlertDialog adb = new AlertDialog.Builder(context).create();
        CardView cardView = (CardView) LayoutInflater.from(context).inflate(R.layout.dialog_hint_common, null);
        adb.setView(cardView);
        adb.show();
//        Window window = adb.getWindow();
//        window.setContentView(R.layout.dialog_hint_common);
        TextView text_ok = (TextView) cardView.findViewById(R.id.text_ok);
        adb.setCancelable(isCancel);
        adb.setCanceledOnTouchOutside(isCancel);
        text_ok.setText("确定");
        text_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adb.dismiss();
            }
        });
        adb.getWindow().setBackgroundDrawable(new ColorDrawable());
        WindowManager.LayoutParams lp = adb.getWindow().getAttributes();
        lp.width = DeviceUtils.getPhoneWidth(context) - 60;//定义宽度
        adb.getWindow().setAttributes(lp);
        adb.show();
        return adb;
    }


    /**
     * 修改本地的锁名称
     *
     * @param context
     * @param e
     */
    public static void editLocalLockName(final Context context, final IOnEditName e) {
        final AlertDialog adb = new AlertDialog.Builder(context).create();
        CardView cardView = (CardView) LayoutInflater.from(context).inflate(R.layout.dialog_edit_name, null);
        adb.setView(cardView);
        adb.show();
        Window window = adb.getWindow();
        window.setContentView(R.layout.dialog_edit_name);
        adb.setCancelable(false);
        adb.setCanceledOnTouchOutside(false);
        final EditText editText = (EditText) window.findViewById(R.id.et_name);
        editText.setHint("请输入名字");
        TextView left = (TextView) window.findViewById(R.id.tv_left);
        left.setText("默认名字");
        TextView right = (TextView) window.findViewById(R.id.tv_right);
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adb.dismiss();
                e.onChange("");
            }
        });
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String msg = editText.getText().toString().trim();
                if (msg.equals("")) {
                    ToastUtils.showToast(context, "请输入锁名称");
                    return;
                }
                e.onChange(msg);
                adb.dismiss();
            }
        });
        adb.getWindow().setBackgroundDrawable(new ColorDrawable());
        WindowManager.LayoutParams lp = adb.getWindow().getAttributes();
        lp.width = DeviceUtils.getPhoneWidth(context) - 60;//定义宽度
        adb.getWindow().setAttributes(lp);
        adb.show();
    }

    public interface IOnEditName {
        void onChange(String msg);
    }

    public static int COMMON_DIALOG_LEFT = 1;
    public static int COMMON_DIALOG_RIGHT = 0;

    public interface IDialogCommonListener {
        void onClick(int direction);
    }


    /**
     * 一般的dialog
     *
     * @param context
     * @param msg
     * @param cancel
     * @param ok
     * @param isCancel 外部点击是否取消
     * @param e
     */
    public static void showCommonDialog(Context context, String msg, String cancel, String ok, boolean isCancel, final IDialogCommonListener e) {
        final AlertDialog adb = new AlertDialog.Builder(context).create();
        CardView cardView = (CardView) LayoutInflater.from(context).inflate(R.layout.dialog_common, null);
        adb.setView(cardView);
        adb.show();
        Window window = adb.getWindow();
        window.setContentView(R.layout.dialog_common);
        TextView left = (TextView) window.findViewById(R.id.tv_left);
        TextView tv_line = (TextView) window.findViewById(R.id.tv_line);
        TextView right = (TextView) window.findViewById(R.id.tv_right);
        TextView tv_msg = (TextView) window.findViewById(R.id.tv_msg);
        adb.setCancelable(isCancel);
        adb.setCanceledOnTouchOutside(isCancel);
        tv_msg.setText(msg);
        if (ok != null && !ok.equals(""))
            right.setText(ok);
        if (cancel != null && !cancel.equals("")) {
            left.setText(cancel);
        } else {
            left.setVisibility(View.GONE);
            tv_line.setVisibility(View.GONE);
        }

        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (e != null)
                    e.onClick(COMMON_DIALOG_RIGHT);
                adb.dismiss();
            }
        });
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (e != null)
                    e.onClick(COMMON_DIALOG_LEFT);
                adb.dismiss();
            }
        });
        adb.getWindow().setBackgroundDrawable(new ColorDrawable());
        WindowManager.LayoutParams lp = adb.getWindow().getAttributes();
        lp.width = DeviceUtils.getPhoneWidth(context) - 60;//定义宽度
        adb.getWindow().setAttributes(lp);
        adb.show();
    }

    /**
     * 一般的dialog
     *
     * @param context
     * @param msg
     * @param cancel
     * @param ok
     * @param isCancel 外部点击是否取消
     * @param e
     */
    public static void showCommonDialogWithTitle(Context context, String titlStre, String msg, String cancel, String ok, boolean isCancel, final IDialogCommonListener e) {
        final AlertDialog adb = new AlertDialog.Builder(context).create();
        CardView cardView = (CardView) LayoutInflater.from(context).inflate(R.layout.dialog_common, null);
        adb.setView(cardView);
        adb.show();
        Window window = adb.getWindow();
        window.setContentView(R.layout.dialog_common);
        TextView title = (TextView) window.findViewById(R.id.title);
        title.setVisibility(View.VISIBLE);
        TextView left = (TextView) window.findViewById(R.id.tv_left);
        TextView tv_line = (TextView) window.findViewById(R.id.tv_line);
        TextView right = (TextView) window.findViewById(R.id.tv_right);
        TextView tv_msg = (TextView) window.findViewById(R.id.tv_msg);
        adb.setCancelable(isCancel);
        adb.setCanceledOnTouchOutside(isCancel);
        title.setText(titlStre);
        tv_msg.setText(msg);
        if (ok != null && !ok.equals(""))
            right.setText(ok);
        if (cancel != null && !cancel.equals("")) {
            left.setText(cancel);
        } else {
            left.setVisibility(View.GONE);
            tv_line.setVisibility(View.GONE);
        }

        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                e.onClick(COMMON_DIALOG_RIGHT);
                adb.dismiss();
            }
        });
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                e.onClick(COMMON_DIALOG_LEFT);
                adb.dismiss();
            }
        });
        adb.getWindow().setBackgroundDrawable(new ColorDrawable());
        WindowManager.LayoutParams lp = adb.getWindow().getAttributes();
        lp.width = DeviceUtils.getPhoneWidth(context) - 60;//定义宽度
        adb.getWindow().setAttributes(lp);
        adb.show();
    }

    public static <T> void showListPopup(final Context mContext, View view, List<T> list) {
        final ListPopupWindow listPopupWindow = new ListPopupWindow(mContext);

        //设置ListView类型的适配器
        listPopupWindow.setAdapter(new ArrayAdapter<T>(mContext, android.R.layout.simple_list_item_1, list));

        //给每个item设置监听事件
        listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ToastUtils.showToast(mContext, "the position is" + position);
//                listPopupWindow.dismiss();
            }
        });

        //设置ListPopupWindow的锚点,也就是弹出框的位置是相对当前参数View的位置来显示，
        listPopupWindow.setAnchorView(view);

        //ListPopupWindow 距锚点的距离，也就是相对锚点View的位置
        listPopupWindow.setHorizontalOffset(0);
        listPopupWindow.setVerticalOffset(0);

        //设置对话框的宽高
        listPopupWindow.setWidth(720);
        listPopupWindow.setHeight(500);
        listPopupWindow.setModal(false);

        listPopupWindow.show();

    }

    static boolean isShow = false;

    public static void showDownLoadProgressDialog(Context mConetext, int progress, String title) {
        if (isShow) {
            dialog.setProgress(progress);
        } else {
            dialog = new ProgressDialog(mConetext);
            dialog.setTitle(title);
            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                dialog.setProgressDrawable(mConetext.getDrawable(R.drawable.progress_dialog_drawable));
            }
            dialog.setProgress(progress);
            dialog.setCancelable(false);
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();
            isShow = true;
        }
    }

    public static void cacelDownLoadProgressDialog() {
        if (dialog != null) {
            dialog.cancel();
            isShow = false;
        }

    }


    /**
     * 弹出选择框
     */
    public static void showPopuWindow(int layoutId, final Activity mContext, View rootView,
                                      View.OnClickListener e, int gravity, int... itemId) {
        PopupWindow mPopuWindow;
        View view;

        LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(layoutId, null);
        mPopuWindow = new PopupWindow(view,  ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopuWindow.setOutsideTouchable(true);
//        mPopuWindow.setAnimationStyle(R.style.my_pop_style_tran);
        for (int id : itemId) {
            view.findViewById(id).setOnClickListener(e);
        }
        mPopuWindow.setFocusable(true);
        mPopuWindow.setOutsideTouchable(true);
        mPopuWindow.setBackgroundDrawable(new BitmapDrawable());
        CommonUtils.backgroundAlpha(mContext, 0.7f);
        mPopuWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                CommonUtils.backgroundAlpha(mContext, 1.0f);
            }
        });
//        mPopuWindow.showAtLocation(rootView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
//        mPopuWindow.showAtLocation(rootView, gravity, 0, 0);
//        mPopuWindow.setWidth(300);
        mPopuWindow.showAsDropDown(rootView);
    }


}
